#include "textmenu.h"
#include "delay.h"
#include "led.h"

unsigned char textMenu_KeyDownCheck(void)
{
    return 0;
}

unsigned char textMenu_KeyDownCallback(void)
{
    textMenu.curMenuID++;
    if (textMenu.curMenuID >= (MAX_MENU - 1)) {
        textMenu.curMenuID = (MAX_MENU - 1);
    }

    return 1;
}

unsigned char textMenu_KeyUpCheck(void)
{
    return 0;
}

unsigned char textMenu_KeyUpCallback(void)
{
    textMenu.curMenuID--;
    if (textMenu.curMenuID < 0) {
        textMenu.curMenuID = 0;
    }

    return 1;
}

unsigned char textMenu_KeySelectCheck(void)
{
    return 0;
}

unsigned char textMenu_KeySelectCallback(void)
{
    return 0;
}



void blink()
{
    while (textMenu.textMenu_KeySelectCheck()) {
        LED1_TOGGLE;
        delay_ms(500);
    }
    delay_ms(1000);
}

void ledoff(void)
{
    LED1_OFF;
    delay_ms(500);
}

void ledon(void)
{
    LED1_ON;
    delay_ms(500);
}

void backLightBrightness(void)
{
    unsigned char brightness = 100;

    lcd.clearBuffer();
    while (digitalRead(select) != 1) {
        if (digitalRead(btnDown)) {
            brightness -= 5;
            delay(200);
        }
        if (digitalRead(btnUp)) {
            brightness += 5;
            delay(200);
        }
        analogWrite(6, brightness);

        lcd.setCursor(5, 25);
        lcd.println( "Brightness");
        lcd.setCursor(5, 40);
        lcd.println( (int)brightness);
        lcd.sendBuffer();
        lcd.clearBuffer();

    }
    delay(1000);
}






void (*pSelectFuncCB[MAX_MENU])(void) = {
    blink,
    ledoff,
    ledon,
    backLightBrightness,
};




void textMenu_Init(pTextMenu_t pTextMenu)
{
    pTextMenu->pTmenuList        = menuItems;
    pTextMenu->textMenuSize      = sizeof(menuItems);
    pTextMenu->curMenuID         = 0;
    pTextMenu->KeyDownCheck      = textMenu_KeyDownCheck;
    pTextMenu->KeyDownCallback   = textMenu_KeyDownCallback;
    pTextMenu->KeyUpCheck        = textMenu_KeyUpCheck;
    pTextMenu->KeyUpCallback     = textMenu_KeyUpCallback;
    pTextMenu->KeySelectCheck    = textMenu_KeySelectCheck;
    pTextMenu->KeySelectCallback = textMenu_KeySelectCallback;
}




